package com.uum.blog.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.blog.pojo.entity.BlogMsg;
import com.uum.blog.pojo.entity.Comment;
import com.uum.blog.service.BlogMsgService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.result.Result;
import com.uum.web.util.IpUtils;
import com.uum.web.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "留言板接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v2/blog-msg")
public class BlogMsgController {

    private final BlogMsgService blogMsgService;

    @ApiOperation(value = "分页获取留言板")
    @GetMapping("page/{currentPage}")
    public Result pageBlogMsg(@PathVariable Long currentPage){
        IPage<BlogMsg> page = blogMsgService.page(new Page<>(currentPage, 10),new LambdaQueryWrapper<BlogMsg>().orderByDesc(BlogMsg::getCreateTime));
        return Result.success(page);
    }

    @ApiOperation(value = "添加留言")
    @PostMapping("add")
    public Result addBlogMsg(@RequestBody BlogMsg blogMsg, HttpServletRequest request){
        if (StrUtil.isBlank(blogMsg.getUserId())) {
            String ip = IpUtils.getIpAddr(request);
            blogMsg.setUserId(ip);
            String cityInfo = IpUtils.getCityInfo(ip);
            if (cityInfo.equals("内网IP|0|0|内网IP|内网IP")) {
                cityInfo = "内网";
            }
            blogMsg.setName(cityInfo);
        }
        boolean save = blogMsgService.save(blogMsg);
        return Result.judge(save).add("obj",blogMsg);
    }

    @ApiOperation(value = "删除留言")
    @DeleteMapping("delete/{id}")
    public Result deleteBlogMsg(@PathVariable Long id){
        boolean result = blogMsgService.removeById(id);
        return Result.judge(result);
    }


}

